package com.net.oil.product;

import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.net.oil.R;
import com.net.oil.app.Constant;
import com.net.oil.base.BaseActivity;
import com.net.oil.product.adapter.ProductRecyclerViewAdapter;
import com.net.oil.product.bean.ProductListBean;
import com.net.oil.product.contract.ProductListContract;
import com.net.oil.product.presenter.ProductListPresenter;

import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ProductListActivity extends BaseActivity<ProductListPresenter> implements ProductListContract.View {

    @BindView(R.id.toolbar_go_back)
    ImageView toolbarGoBack;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.product_recycler_view)
    RecyclerView productRecyclerView;

    private ProductRecyclerViewAdapter adapter;

    private int categoryId = -1;     //分类id
    private int page = 1;           //当前页
    private int rows = 20;          //分页大小
    private boolean haveMore = true;

    private ProductListBean productListBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_list);
        ButterKnife.bind(this);
        presenter = new ProductListPresenter(this);
        initView();
        initData();
    }

    private void initData() {
        categoryId = getIntent().getIntExtra(Constant.INTENT_PRODUCT_CLASS_ID, -1);
        if (categoryId != -1){
            showProgressDialog();
            presenter.getProductList(categoryId, "", page, rows);
        }
//        productListBean = (ProductListBean) getIntent().getSerializableExtra(Constant.INTENT_PRODUCT_LIST_KEY);
//
//        if (productListBean != null){
//            if (productListBean.getList() != null){
//                if (page == 1){
//                    adapter.updateData(productListBean.getList());
//                }else if (page > 1){
//                    adapter.loadMore(productListBean.getList());
//                }
//            }
//
//            if (productListBean.getPaging() != null){
//                if (productListBean.getPaging().getTotal() == adapter.getItemCount() - 1){
//                    adapter.changeMoreStatus(Constant.LOADING_NONE);
//                    haveMore = false;
//                }else {
//                    adapter.changeMoreStatus(Constant.PULL_LOAD_MORE);
//                    haveMore = true;
//                    page++;
//                }
//            }
//        }
    }

    private void initView() {
        toolbarTitle.setText("产品列表");

        adapter = new ProductRecyclerViewAdapter(this);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        productRecyclerView.setLayoutManager(layoutManager);
        productRecyclerView.addItemDecoration(new DividerItemDecoration(this, layoutManager.getOrientation()));
        productRecyclerView.setAdapter(adapter);

        productRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (!productRecyclerView.canScrollVertically(1) && haveMore && page > 1){
                    adapter.changeMoreStatus(Constant.LOADING_MORE);
                    presenter.getProductList(categoryId, "", page, rows);
                }
            }
        });
    }

    @OnClick(R.id.toolbar_go_back)
    public void onViewClicked() {
        finish();
    }

    /**
     * 获取产品列表成功
     * @param productListBeen   产品列表数据
     */
    @Override
    public void getProductListSuccess(ProductListBean productListBeen) {
        dismissProgressDialog();
        if (productListBeen != null){
            if (productListBeen.getList() != null && productListBeen.getList().size() > 0){
                if (page == 1){
                    adapter.updateData(productListBeen.getList());
                }else if (page > 1){
                    adapter.loadMore(productListBeen.getList());
                }
                adapter.changeMoreStatus(Constant.PULL_LOAD_MORE);
                haveMore = true;
                page++;
            }else {
                adapter.changeMoreStatus(Constant.LOADING_NONE);
                haveMore = false;
            }

            /*if (productListBeen.getPaging() != null){
                if (productListBeen.getPaging().getTotal() == adapter.getItemCount() - 1){
                    adapter.changeMoreStatus(Constant.LOADING_NONE);
                    haveMore = false;
                }else {
                    adapter.changeMoreStatus(Constant.PULL_LOAD_MORE);
                    haveMore = true;
                    page++;
                }
            }*/
        }
    }

    /**
     * 获取产品列表失败
     */
    @Override
    public void getProductListFailed() {
        dismissProgressDialog();
        adapter.changeMoreStatus(Constant.LOADING_FAIL);
    }
}
